
"  1. 类似vscode的terminal - 已实现
"  2. 模糊查找
"  3. goto definition - 已实现
call plug#begin('~/.vim/plugged')
" === auto-pairs
Plug 'jiangmiao/auto-pairs'
" === cmake support
Plug 'cdelledonne/vim-cmake'
" === 文件管理器nerdtree
Plug 'preservim/nerdtree'
" === nerdtree相关插件
Plug 'ryanoasis/vim-devicons'
Plug 'tiagofumo/vim-nerdtree-syntax-highlight'

" === markdown
" Plug 'mzlogin/vim-markdown-toc'
" Plug 'iamcco/markdown-preview.nvim', {'do': { -> mkdp#util#install()}, 'for': ['markdown', 'vim-plug']}


" === coc
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" === c++ syntax highlight
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
" === snippets
Plug 'honza/vim-snippets'
" === 主题
Plug 'morhetz/gruvbox'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'tomasiser/vim-code-dark'
"Plug 'octol/vim-cpp-enhanced-highlight'
Plug 'Yggdroot/indentLine'
Plug 'jaxbot/semantic-highlight.vim'
" === doxy documentation
"Plug 'vim-scripts/DoxygenToolkit.vim'
call plug#end()

set encoding=UTF-8
" 主题设置
let g:db_ui_use_nerd_fonts=1
set cursorline
autocmd vimenter * ++nested colorscheme gruvbox
colorscheme gruvbox
" airline主题
let g:airline_theme = 'bubblegum'
" airline Tab切换窗口
let g:airline#extensions#tabline#enabled = 1

" NerdTree
map <c-n> :NERDTreeToggle<CR>
"打开vim时如果没有文件自动打开NERDTree
autocmd vimenter * if !argc()|NERDTree|endif
"当NERDTree为剩下的唯一窗口时自动关闭
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" NerdTree icon
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let g:NERDTreeShowLineNumbers = 0
let g:NERDTreeHidden = 0
let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1
let g:semanticEnableFileTypes = ['cpp', 'c', 'python']
" Tab宽度
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4

" 变量高亮设置
let g:semanticTermColors = [12]
" === 平时尽量在非插入模式下，这样能降低效率损失
set updatetime=300
autocmd CursorHold *.cpp,*.c,*.h,*.py SemanticHighlight
" === 快捷键设置
map <C-l> <C-w>l
map <C-h> <C-w>h:silent NERDTreeRefreshRoot<cr>
map <C-j> <C-w>j
map <C-k> <C-w>k
nmap ss :split<cr><C-w>w
nmap sv :vsplit<cr><C-w>w
nmap Q :q<cr>

nmap <C-left> <c-w><
nmap <C-right> <c-w>>
nmap <C-up> <c-w>+
nmap <C-down> <c-w>-
" 打开终端
nmap <A-t> :belowright split<cr> :terminal<cr>
" airline tab切换
nmap <S-tab> :bprev<cr>
nmap <tab> :bnext<cr>
" 记住cmake绑定的快捷键
let g:cmake_default_config = 'build'
let g:cmake_root_markers = ['.']
autocmd vimenter *.cpp,*.c,CMakeLists.txt,*.h :CMakeFindDir<cr>
nmap <C-b> :CMakeGenerate<cr>:CMakeBuild<cr>
nmap <C-c> :CMakeClose<cr>
nmap <C-A-b> :CMakeFindDir
" cg = CMakeGenerate
" cb = CMakeBuild
" ci = CMakeInstall
" cq = Close CMake console
" Ctrl+c = Stop current command
" 使用鼠标
set mouse=a
" 显示行号和相对行号
set nu
set rnu
noremap L $
noremap H ^
noremap <s-j> 5j
noremap <s-k> 5k
" 全选功能
nmap <c-a> gg<s-v>G
" goto 代码导航
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gr <Plug>(coc-references)
" markdown命令
" map :markdown-preview :CocCommand markdown-preview-enhanced.openPreview
" map :markdown-sync-preview :CocCommand markdown-preview-enhanced.syncPreview

" syntax highlight
lua <<EOF
require'nvim-treesitter.configs'.setup {
    ensure_installed = {'c', 'cpp', 'python', 'bash', 'cmake'}, 
    highlight = {
        enable = true,
    }
}
-- integrate with rainbow
require "nvim-treesitter.highlight"
local hlmap = vim.treesitter.highlighter.hl_map
hlmap.error = nil
hlmap["punctuation.delimiter"] = "Delimiter"
hlmap["punctuation.bracket"] = nil
EOF

" Coc settings
" Add (Neo)Vim's native statusline support.
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline.
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of language server.
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)
noremap <silent> <C-e> :CocDiagnostics<cr>
" Remap <C-f> and <C-b> for scroll float windows/popups.
if has('nvim-0.4.0') || has('patch-8.2.0750')
  nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
  vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
endif
" Use K to show documentation in preview window.
nnoremap <silent> <C-d> :call <SID>show_documentation()<CR>

function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  elseif (coc#rpc#ready())
    call CocActionAsync('doHover')
  else
    execute '!' . &keywordprg . " " . expand('<cword>')
  endif
endfunction

" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming.
nmap <C-r> <Plug>(coc-rename)
" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup
" TextEdit might fail if hidden is not set.
set hidden


source ~/.config/nvim/plugins/vim-airline.rc.vim
source ~/.config/nvim/plugins/coc.rc.vim
